package com.quantdo.apex.elasticjob.config;

import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.quantdo.apex.elasticjob.constant.JobConfigBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenkuan
 * @version v1.0
 * @desc ElasticJob配置
 * @date 2019/12/11 011 9:52
 */
@Configuration
public class ElasticJobConfig {

	private final JobConfigBean config;

	public ElasticJobConfig(JobConfigBean jobConfigBean) {
		this.config = jobConfigBean;
	}

	@Bean
	public ZookeeperConfiguration zookeeperConfiguration() {
		ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(config.getZookeeperServer(), config.getNamespace());
		zkConfig.setBaseSleepTimeMilliseconds(config.getBaseSleepTimeMilliseconds());
		zkConfig.setMaxSleepTimeMilliseconds(config.getMaxSleepTimeMilliseconds());
		zkConfig.setMaxRetries(config.getMaxRetries());
		zkConfig.setSessionTimeoutMilliseconds(config.getSessionTimeoutMilliseconds());
		zkConfig.setConnectionTimeoutMilliseconds(config.getConnectionTimeoutMilliseconds());
		return zkConfig;
	}

	@Bean(initMethod = "init")
	public CoordinatorRegistryCenter zookeeperRegistryCenter(ZookeeperConfiguration config) {
		return new ZookeeperRegistryCenter(config);
	}
}
